package com.hbfu.zggdwlzzsx.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbfu.zggdwlzzsx.entity.Works;
import com.hbfu.zggdwlzzsx.mapper.CategoryMapper;
import com.hbfu.zggdwlzzsx.mapper.WorkCategoriesMapper;
import com.hbfu.zggdwlzzsx.mapper.WorkMapper;
import com.hbfu.zggdwlzzsx.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WorkServiceImpl extends ServiceImpl<WorkMapper, Works> implements WorkService {
    @Autowired
    private WorkMapper workMapper;
/*

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private WorkCategoriesMapper workCategoriesMapper;
*/
    @Override
    public Works getWorkById(Integer id) {
        return workMapper.getWorkById(id);

    }

    @Override
    public List<Works> getWorks(String name, Integer categoryId) {
        QueryWrapper<Works> queryWrapper = new QueryWrapper<>();
        // 如果 name 不为空，添加 name 模糊查询条件
        if (name != null && !name.isEmpty()) {
            queryWrapper.like("name", name);
        }

        // 如果 categoryId 不为空，通过关联表查询对应作品
        if (categoryId != null) {
            queryWrapper.inSql("id", "SELECT workId FROM workcategories WHERE categoryId = " + categoryId);
        }

        return workMapper.selectList(queryWrapper);
    }

}
